<!-- subpkg_archive/list/index.vue -->
<script lang="ts" setup>
  import { ref, onMounted } from 'vue'
  import { patientListApi, removePatientApi } from '@/apis/patient'
  import type { PatientItem } from '@/types/patientItem'
  import { onShow } from '@dcloudio/uni-app'
  import { useConsultStore } from '@/store/consult'
  // 接收参数
  const props = defineProps<{ type?: 'consult' }>()

  if (props.type === 'consult') {
    uni.setNavigationBarTitle({ title: '选择患者' })
  }

  const store = useConsultStore()
  const selPatient = (id: string) => {
    if (props.type) {
      store.patientId = id
    }
  }
  const swipeOptions = ref([
    {
      text: '删除',
      style: {
        backgroundColor: '#dd524d',
        color: '',
        fontSize: '',
      },
    },
  ])

  // 患者列表数据
  const patientList = ref<PatientItem[]>([])

  // 获取患者列表
  const getPatientList = async () => {
    const res = await patientListApi()
    patientList.value = res
  }
  onShow(() => {
    getPatientList()
  })
  const onSwipeActionClick = (id: string, index: number) => {
    if (props.type !== 'consult') return
    if (store.patientId === id) store.patientId = ''
    removePatientApi(id)
    patientList.value.splice(index)
    uni.showToast({
      title: '删除成功',
      icon: 'none',
    })
  }

  const go = () => {
    if (!store.patientId)
      return uni.showToast({
        title: '请选择患者!',
      })
    uni.navigateTo({ url: '/subpkg_consult/payment/payment' })
  }
</script>

<template>
  <scroll-page>
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>
      <uni-swipe-action>
        <uni-swipe-action-item
          v-for="(item, index) in patientList"
          :key="item.id"
          :right-options="swipeOptions"
          @click="onSwipeActionClick(item.id, index)"
        >
          <view
            @click="selPatient(item.id)"
            :class="{ active: store.patientId === item.id && props.type === 'consult' }"
            class="archive-card"
          >
            <view class="archive-info">
              <text class="name">{{ item.name }}</text>
              <text class="id-card">{{
                item.idCard.replace(/(\d{6})\d{8}(\d{4})/, '$1******$2')
              }}</text>
              <text v-if="item.defaultFlag === 1" class="default">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.genderValue }}</text>
              <text class="age">{{ item.age }}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/form/form?id=${item.id}`"
            >
              <uni-icons type="icon-edit" size="20" color="#16C2A3" custom-prefix="iconfont" />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="patientList.length <= 6" class="archive-card">
        <navigator class="add-link" hover-class="none" url="/subpkg_archive/form/form">
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
      <!-- 下一步 -->
      <view v-if="props.type && store.patientId">
        <button @click="go" :style="{ backgroundColor: '#16C2A3', color: '#fff' }">下一步</button>
      </view>
    </view>
  </scroll-page>
</template>

<style lang="scss">
  @import './list.scss';
</style>
